package com.xxx.modules.sys.service.impl;

import com.xxx.modules.sys.domain.SysUserDomain;
import com.xxx.modules.sys.domain.TbTokenDomain;
import com.xxx.modules.sys.service.ShiroService;
import com.xxx.modules.sys.service.SysUserService;
import com.xxx.modules.sys.service.TbTokenService;
import com.xxx.modules.sys.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by ChenXuefei on 2017/10/12.
 */
public class ShiroServiceImpl implements ShiroService {

    @Autowired
    private TbTokenService tbTokenService;
    @Autowired
    private UserService userService;
    @Autowired
    private SysUserService sysUserService;

    @Override
    public Set<String> getUserPermissions(Long userId) {
        List<String> permsList = userService.queryAllPerms(userId);
        Set<String> permsSet = new HashSet<>();
        if(permsList.size()>0){
            for (String perm : permsList) {
                if(StringUtils.isBlank(perm)) {
                    continue;
                }
                permsSet.addAll(Arrays.asList(perm.split(",")));
            }
        }
        return permsSet;
    }

    @Override
    public TbTokenDomain queryByToken(String accessToken) {
        TbTokenDomain tbTokenDomain = new TbTokenDomain();
        tbTokenDomain.setToken(accessToken);
        return tbTokenService.queryByList(tbTokenDomain).get(0);
    }

    @Override
    public SysUserDomain queryUser(Long userId) {
        return sysUserService.queryById(userId);
    }
}
